package cn.school.student.service.impl;


import cn.school.common.api.CommonPage;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.ConsultPlanInfoDao;
import cn.school.student.dao.ConsultPlanPersonDao;
import cn.school.student.service.ConsultPlanInfoService;
import cn.school.student.vo.ConsultPlanInfoVO;
import cn.school.student.vo.ConsultPlanPersonVO;
import cn.school.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ConsultPlanInfoServiceImpl extends ServiceImpl<ConsultPlanInfoDao, ConsultPlanInfoVO> implements ConsultPlanInfoService {

    @Autowired
    private ConsultPlanInfoDao consultPlanInfoDao;

    @Autowired
    private ConsultPlanPersonDao consultPlanPersonDao;

    @Override
    public CommonPage<Map<String,Object>> getPlanInfoPageList(ConsultPlanInfoVO vo) {
        int count =  consultPlanInfoDao.getPlanInfoPageListCount(vo);
        int startNum=(vo.getPageNum()-1)*vo.getPageSize();
        SysUser user = SysUserUtils.getSysUser();
        vo.setPid("0");
        List<Map<String,Object>> commentList =  consultPlanInfoDao.getPlanInfoPageList(vo, startNum, vo.getPageSize());
        for(Map<String,Object> planMap : commentList){
            if("yes".equals(planMap.get("isChild"))){
                ConsultPlanInfoVO pVO = new ConsultPlanInfoVO();
                pVO.setPid(planMap.get("id")+"");
                List<Map<String,Object>> childList =  consultPlanInfoDao.getPlanInfoPageList(pVO, 0, Integer.MAX_VALUE);
                planMap.put("children",childList);

            }
        }
        IPage<Map<String,Object>> page = new Page<>();
        page.setTotal(count);
        page.setRecords(commentList);
        return CommonPage.restPage(page);
    }

    @Override
    public int addOrupdatePlanInfo(ConsultPlanInfoVO vo) {
        String currUser = SysUserUtils.getSysUser().getUsername();
        int num = 0;
        if(StringUtils.isEmpty(vo.getId())){
            //新增
            vo.setCreateBy(currUser);
            vo.setCreateTime(new Date());
            num = consultPlanInfoDao.insert(vo);

        }else{
            //修改
            vo.setModifyBy(currUser);
            vo.setModifyTime(new Date());
            num = consultPlanInfoDao.updateById(vo);
            //删除之前保存的个人计划
            LambdaQueryWrapper<ConsultPlanPersonVO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ConsultPlanPersonVO::getPlanId,vo.getId());
            consultPlanPersonDao.delete(wrapper);
        }
        if(vo.getPersonList()!= null && vo.getPersonList().size()>0){
            //添加计划人员
            for(ConsultPlanPersonVO personVO : vo.getPersonList()){
                personVO.setCreateBy(currUser);
                personVO.setCreateTime(new Date());
                personVO.setPlanId(vo.getId());
                consultPlanPersonDao.insert(personVO);
            }
        }

        return num;
    }

    @Override
    public int deletePlanInfo(ConsultPlanInfoVO vo) {
        int delPlanFlag = consultPlanInfoDao.deleteById(vo.getId());
        LambdaQueryWrapper<ConsultPlanPersonVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConsultPlanPersonVO::getPlanId,vo.getId());
        consultPlanPersonDao.delete(wrapper);
        return delPlanFlag;
    }

    @Override
    public List<Map<String, Object>> getParentPlanList(ConsultPlanInfoVO vo) {
        List<Map<String, Object>> list = consultPlanInfoDao.getPlanInfoPageList(vo,0,Integer.MAX_VALUE);
        return list;
    }

    @Override
    public List<ConsultPlanPersonVO> getPersonListByPlanId(String planId) {
        LambdaQueryWrapper<ConsultPlanPersonVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConsultPlanPersonVO::getPlanId,planId);
        List<ConsultPlanPersonVO> list = consultPlanPersonDao.selectList(wrapper);
        return list;
    }
}
